﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgileDashboard.Entities;
using AgileDashboard.Data;
using PlanningPokerMVC.Helpers;
using Newtonsoft.Json;
using AgileDashboard.Services;
using PlanningPokerMVC.Models;

namespace PlanningPokerMVC.Controllers
{ 
    public class VoteController : Controller
    {
        private PlanningPokerContext db = new PlanningPokerContext();

        //
        // GET: /Vote/

        public ViewResult Index()
        {
            return View(db.VoteDetails.ToList());
        }

        //
        // GET: /Vote/Details/5

        public ViewResult Details(int id)
        {
            VoteDetail votedetail = db.VoteDetails.Find(id);
            return View(votedetail);
        }

        //
        // GET: /Vote/Create

        public ActionResult Create(int workItemId)
        {
            WorkItem wi = db.WorkItems.Find(workItemId);

            VoteDetail vote = new VoteDetail();
            vote.UserName = User.Identity.Name;
            vote.WorkItem = wi;
            vote.CreationTimeStamp = DateTime.Now;

            return View(vote);
        } 

        //
        // POST: /Vote/Create

        [HttpPost]
        public ActionResult Create(VoteDetail votedetail)
        {
            if (ModelState.IsValid)
            {


                
                WorkItem wi = db.WorkItems.Find(votedetail.WorkItem.Id);
                votedetail.WorkItem = wi;
                wi.VoteDetails.Add(votedetail);

                votedetail.UserName = User.Identity.Name;
                votedetail.CreationTimeStamp = DateTime.Now;
                
                db.SaveChanges();
                return RedirectToAction("Index","PlanningPoker");  
            }

            return View(votedetail);
        }
        
        //
        // GET: /Vote/Edit/5
 
        public ActionResult Edit(int id)
        {
            VoteDetail votedetail = db.VoteDetails.Find(id);
            return View(votedetail);
        }



        public JsonNetResult VoteDetail(int workItemId)
        {

            WorkItemService service = new WorkItemService();

            List<VoteDetail> voteDetails = service.GetVoteDetails(workItemId);
            List<VoteResultViewModel> voteResults = new List<VoteResultViewModel>();

            foreach (VoteDetail vd in voteDetails)
            {
                voteResults.Add(new VoteResultViewModel(vd));

            }


            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Formatting = Formatting.Indented;
            jsonNetResult.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsonNetResult.Data = voteResults;

            return jsonNetResult;
        }



        //
        // POST: /Vote/Edit/5

        [HttpPost]
        public ActionResult Edit(VoteDetail votedetail)
        {
            if (ModelState.IsValid)
            {
                db.Entry(votedetail).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(votedetail);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}